<?php
/*
 * @Author: lokei
 * @Date: 2022-09-19 13:45:10
 * @LastEditors: lokei
 * @LastEditTime: 2022-11-25 00:39:44
 * @Description: 
 */
namespace App\Http\Controllers\Topic;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Cms\Comment\CommentModel;
use App\Models\Topic\TopicModel;
use Illuminate\Http\Request;

class CommentController extends Controller
{
	public function submit(Request $request)
	{
		$user = app('auth')->user();
		$comment = new CommentModel();
		$comment->uid = $user->uid;
		$comment->user_info = json_encode([
			'name' => $user->nickname,
			'avatar' => $user->avatar
		]);
		$comment->topic_id = $request->input('topic_id');
		$comment->content = $request->input('content');
		$comment->created_at = date('Y-m-d H:i:s', time());
		$comment->save();
		$topic = TopicModel::where('id', '=', $request->input('topic_id'))->first();
		if(!is_null($topic)) {
			$topic->comment_num = $topic->comment_num + 1;
			$topic->save();
		}
		$res = ResultTool::success();
		return $res;
	}
}
